﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NETCORE.Larger.Data.common;
using NETCORE.Larger.Data.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace NETCORE.Larger.Data.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BooksController : ControllerBase
    {
        private readonly DataBase db;

        public BooksController(DataBase context)
        {
            this.db = context;
        }



        [Route("GetOneBook")]
        [HttpGet]
        public ActionResult<List<TE_Book>> GetOneBook()
        {
            TE_Book tbook = new TE_Book();

            var arr = db.Set<TE_Book>().Where(t => t.price == 13).ToList();

            return Ok(arr);
        }



        private static List<TE_Book> textDataArr = null;
        private List<TE_Book> GetTextDataArr()
        {
            if (textDataArr == null)
            {
                textDataArr = new List<TE_Book>();
                for (int i = 0; i < 100000; i++)
                {
                    textDataArr.Add(new TE_Book()
                    {
                        id = Guid.NewGuid().ToString(),
                        name = Persion.GetOneName(),
                        introduction = Persion.GetOneParagraph(),
                        price = new Random().Next(10000) / 100,
                        bookstoreid = null,
                        used = Persion.GetOneParagraph(),
                        deleteuserid = null,
                        author = Persion.GetOneName()
                    });
                }
            }
            return textDataArr;
        }

        //10w数据 8.53秒
        [Route("InsertBookBatch")]
        [HttpPost]
        public async Task<ActionResult<int>> InsertBookBatch()
        {
            var arr = GetTextDataArr();
           
            var tstart = DateTime.Now;

            Console.WriteLine("开始：" + tstart.ToLongTimeString());

            await db.Set<TE_Book>().AddRangeAsync(arr);

            await db.SaveChangesAsync();

            var tend = DateTime.Now;

            Console.WriteLine("结束：" + tend.ToLongTimeString() + "，耗时：" + (tend - tstart).ToString());

            return 1;
        }


    }
}
